package com.dean.utils;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeanXmlParseUitls {
    private static final Logger  logger = LoggerFactory.getLogger(DeanXmlParseUitls.class);

    public static List<Map<String,String>> docParse2Map(String filePath,String loopNode) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(filePath));
        Element root = document.getRootElement();
        List<Map<String,String>> results = new ArrayList<Map<String,String>>();
        DeanXmlParseUitls.nodeParse2Map(root,loopNode,root.getName(),results);
        return results;
    }

    public static List<List<String>> docParse2List(String filePath,String loopNode) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(filePath));
        Element root = document.getRootElement();
        List<List<String>> results = new ArrayList<List<String>>();
        DeanXmlParseUitls.nodeParse2List(root,loopNode,root.getName(),results);
        return results;
    }
    public static void nodeParse2Map(Element root, String loopNode, String currNode,List<Map<String,String>> results){
        logger.info("root:{},curr:{}",root.getName(),currNode);
        // 如果完全匹配上了
        if(loopNode.equals(currNode)){
            Map<String,String> result = new HashMap<String,String>();
            List<Element> elements = root.elements();
            elements.forEach(element -> {
                result.put(element.getQualifiedName(),element.getStringValue());
            });
            results.add(result);
            // 需要继续遍历节点
        }else if(loopNode.contains(currNode)){
            List<Element> elements = root.elements();
            elements.forEach(element -> {
                nodeParse2Map(element,loopNode,currNode+"/"+element.getName(),results);
            });
        }
    }

    public static void nodeParse2List(Element root, String loopNode, String currNode,List<List<String>> results){
        logger.info("root:{},curr:{}",root.getName(),currNode);
        // 如果完全匹配上了
        if(loopNode.equals(currNode)){
            List<String> result = new ArrayList<String>();
            List<Element> elements = root.elements();
            // 如果表为空时 添加表头
            if(results.isEmpty()){
                List<String> head = new ArrayList<String>();
                elements.forEach(element -> {
                    head.add(element.getName());
                    result.add(element.getStringValue());
                });
                results.add(head);
            }else{
                elements.forEach(element -> {
                    result.add(element.getStringValue());
                });
            }
            results.add(result);
            // 需要继续遍历节点
        }else if(loopNode.contains(currNode)){
            List<Element> elements = root.elements();
            elements.forEach(element -> {
                nodeParse2List(element,loopNode,currNode+"/"+element.getName(),results);
            });
        }
    }

}
